/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vasthu.api.domain;

/**
 *
 * @author moremada
 *
 * This class holds information about the quantity of which purchased from the
 * available stock
 */
public class PurchasedItem {

    private Stock stockItem;
    private float quantity;
    private float discountRate;
    private float subTotal;
    private float tax;
    private int rowNumber;

    /**
     * @return the stockItem
     */
    public Stock getStockItem() {
        return stockItem;
    }

    /**
     * @param stockItem the stockItem to set
     */
    public void setStockItem(Stock stockItem) {
        this.stockItem = stockItem;
        this.discountRate = stockItem.getDiscount();
    }

    /**
     * @return the quantity
     */
    public float getQuantity() {
        return quantity;
    }

    /**
     * @param quantity the quantity to set
     */
    public void setQuantity(float quantity) {
        if(quantity >= 0 && stockItem.getQuantity() >= quantity){
            //if(quantity > this.quantity){
                this.quantity = quantity;
            //}
            //else if(quantity < this.quantity){
            //    this.quantity = quantity;
            //}
        }
    }

    public void updateStockQuantity() {
        stockItem.setQuantity((stockItem.getQuantity() - quantity));
    }

    /**
     * @return the discountRate
     */
    public float getDiscountRate() {
        return discountRate;
    }

    /**
     * @param discountRate the discountRate to set
     */
    public void setDiscountRate(float discountRate) {
        this.discountRate = discountRate;
    }

    /**
     * @return the subTotal
     */
    public float getSubTotal() {
        return subTotal;
    }

    /**
     * @param subTotal the subTotal to set
     */
    public void setSubTotal(float subTotal) {
        this.subTotal = subTotal;
    }

    public float calculateSubTotal() {
        //updateStockQuantity();
        float unitPrice = stockItem.getUnitPrice();
        float discountAmount = discountRate * unitPrice / 100;
        float taxAmount = tax * unitPrice / 100;
        float tempUnitPrice = unitPrice - discountAmount + taxAmount;
        subTotal = tempUnitPrice * quantity;
        return subTotal;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PurchasedItem other = (PurchasedItem) obj;
        if (this.discountRate == other.getDiscountRate() && this.quantity == other.getQuantity()
                && this.stockItem.getStockId() == other.getStockItem().getStockId()
                && this.subTotal == other.getSubTotal() && other.getRowNumber() == this.rowNumber) {
            return true;
        }
        return false;
    }

    /**
     * @return the rowNumber
     */
    public int getRowNumber() {
        return rowNumber;
    }

    /**
     * @param rowNumber the rowNumber to set
     */
    public void setRowNumber(int rowNumber) {
        this.rowNumber = rowNumber;
    }
}
